﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.RequestOfficeSupplies.DAL;
using XQ.RequestOfficeSupplies.DAL.DAL;
using XQ.RequestOfficeSupplies.DAL.Model;

namespace XQ.RequestOfficeSupplies.BLL.Sys
{
    public class DictBLL
    {
      private readonly DictDal dal = new DictDal();
        public List<DictModel> GetDictList(int categoryId)
        {
            List<DictModel> dicts= dal.GetDictList(categoryId);
            foreach (DictModel d in dicts)
            {
                d.Childrens = GetChildrens(d);
            }
            return dicts;
        }

        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public List<DictModel> GetChildrens(DictModel dict)
        {
            List<DictModel> dicts = dal.GetChildrens(dict.ID);
            foreach (DictModel d in dicts)
            {
                d.Childrens = GetChildrens(d);
            }
            return dicts;
        }

        /// <summary>
        /// 根据字典分类Code获取当前分类的字典列表
        /// </summary>
        /// <returns></returns>
        public List<DictModel> GetDictByCode(string categoryCode)
        {
            return dal.GetDictByCode(categoryCode);
        }

        /// <summary>
        /// 根据字典分类Code获取当前分类的字典列表
        /// </summary>
        /// <returns></returns>
        public List<DictModel> GetAllDictByCode(string categoryCode)
        {
            List<DictModel> dicts = dal.GetDictByCode(categoryCode);
            foreach (DictModel d in dicts)
            {
                d.Childrens = GetChildrens(d);
            }
            return dicts;
        }

        /// <summary>
        /// 根据ID获取字典类型
        /// </summary>
        /// <param name="dictId"></param>
        /// <returns></returns>
        public Sys_Dics GetDictById(int dictId)
        {
            return dal.GetDictById(dictId);
        }

        /// <summary>
        /// 根据编号获取字典名称
        /// </summary>
        /// <param name="dictId"></param>
        /// <returns></returns>
        public Sys_Dics GetDictNameByCode(string code)
        {
            return dal.GetDictNameByCode(code);
        }

        /// <summary>
        /// 保存字典类型
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool SaveDict(Sys_Dics dic)
        {
            if (dic.ID != 0)
            {
                return dal.UpdateDict(dic);
            }
            return dal.SaveDict(dic);
        }

        public bool DeleteDict(int dictId)
        {
            return dal.DeleteDict(dictId);
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        public bool DelDicts(List<int> dictIds)
        {
            return dal.DelDicts(dictIds);
        }

        public int GetDicIDByTitle(string title)
        {
            return dal.GetDicIDByTitle(title);
        }

        public int GetDictIDByTitle(string title)
        {
            return dal.GetDictIDByTitle(title);
        }
        public List<Sys_Dics> GetDictValue(int? CategoryId)
        {
            return dal.GetDictValue(CategoryId);
        }

        /// <summary>
        /// 获取多个职务名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetDuties(string ids)
        {
            string reVal = string.Empty;
            if (!string.IsNullOrEmpty(ids))
            {
                List<Sys_Dics> list = dal.GetDicsByIds(ids);
                foreach (var d in list)
                {
                    if (reVal != string.Empty)
                    {
                        reVal += "," + d.Title;
                    }
                    else
                    {
                        reVal = d.Title;
                    }
                }
            }
            return reVal;
        }
    }
 

}
